const network = require('../../utils/network')

Page({
  data: {
    phoneText: '',
    totpText: '',
    passwordText: '',
    totpStatus: 0, // 0 未发送，1 已发送
    totpBtnText: '',
    block: false
  },
  onLoad() {
    wx.setNavigationBarTitle({
      title: '找回密码'
    })
    let user = wx.getStorageSync('user')
    if (user) {
      this.setData({
        phoneText: user.tel
      })
    }
  },

  /**
   * @desc 验证
   */
  validate() {
    let phoneReg = /^1\d{10}$/
    let totpReg = /^\d{6}$/
    let passwordReg = /^\w{6,16}$/

    // 电话号码格式检测
    if (!phoneReg.test(this.data.phoneText.trim())) {
      wx.showModal({
        title: '提示',
        content: '请填写正确的手机号',
        showCancel: false
      })
      return false
    }
    // 验证码格式检测
    if (!totpReg.test(this.data.totpText.trim())) {
      wx.showModal({
        title: '提示',
        content: '请填写正确的验证码',
        showCancel: false
      })
      return false
    }

    // 密码格式检测
    if (!passwordReg.test(this.data.passwordText.trim())) {
      wx.showModal({
        title: '提示',
        content: '请填写 6 至 16 位密码',
        showCancel: false
      })
      return false
    }

    return true
  },

  /**
   * @desc 注册
   */
  register() {
    if (this.validate()) {
      network
        .post('game/resetPwd', {
          userName: 'S' + this.data.phoneText.trim(),
          pwd: this.data.passwordText.trim(),
          code: this.data.totpText.trim()
        })
        .then(data => {
          if (data.status == 1) {
            wx.showToast({
              title: '修改成功',
              icon: 'none',
              duration: 2000
            })
            setTimeout(_ => {
              wx.reLaunch({
                url: '/pages/login/login'
              })
            }, 2000)
          } else {
            wx.showToast({
              title: data.results,
              icon: 'none',
              duration: 2000
            })
          }
        })
        .catch(err => {
          console.log(err)
          wx.showToast({
            title: err.data.msg,
            icon: 'none',
            duration: 2000
          })
        })
    }
  },

  /**
   * @desc 获取验证码
   */
  getTotp() {
    let phoneReg = /^1\d{10}$/

    // 电话号码格式检测
    if (!phoneReg.test(this.data.phoneText.trim())) {
      wx.showModal({
        title: '提示',
        content: '请填写正确的手机号',
        showCancel: false
      })
      return false
    }

    if (!this.data.block) {
      this.setData({
        block: true
      })
      network
        .post('game/sendCode', {
          userName: 'S' + this.data.phoneText.trim()
        })
        .then(data => {
          this.setData({
            block: false
          })
          wx.showToast({
            title: '发送成功',
            icon: 'none',
            duration: 2000
          })
          this.countDown()
        })
        .catch(err => {
          this.setData({
            block: false
          })
          console.log(err)
          wx.showToast({
            title: err.data.msg,
            icon: 'none',
            duration: 2000
          })
        })
    }
  },

  /**
   * @desc 验证码倒计时
   */
  countDown() {
    let count = 90
    this.setData({
      totpStatus: 1,
      totpBtnText: count + '秒'
    })

    let time = setInterval(e => {
      count--
      if (count > 0) {
        this.setData({
          totpBtnText: count + '秒'
        })
      } else {
        this.setData({
          totpBtnText: '',
          totpStatus: 0
        })
        clearInterval(time)
      }
    }, 1000)
  },

  /**
   * @desc 手机号输入
   * @param e
   */
  phoneInput(e) {
    let phoneText = e.detail.value.trim()
    this.setData({
      phoneText
    })
  },

  /**
   * @desc 验证码失焦
   * @param e
   */
  totpBlur(e) {
    let totpText = e.detail.value.trim()
    this.setData({
      totpText
    })
  },

  /**
   * @desc 密码输入
   * @param e
   */
  passwordInput(e) {
    let passwordText = e.detail.value.trim()
    this.setData({
      passwordText
    })
  }

})